package com.xiaoyi.picshow;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.avin.yi.R;
import com.bumptech.glide.Glide;

import com.xiaoyi.entity.XyData;
import com.xiaoyi.tool.UrlTool;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;
import uk.co.senab.photoview.PhotoView;

/**
 大图 加载viewpager 适配器 。
 *
 */

public class BigPhotoPagerAdapter extends PagerAdapter {

    private Context mCtx ;

    private List<XyData> mItems = new ArrayList<>();

    public BigPhotoPagerAdapter(Context mCtx  ,List<XyData> mItems )  {
        this.mCtx = mCtx ;
        this.mItems = mItems ;

    }


    @Override
    public int getCount() {
        int size  =   mItems==null ? 0 : mItems.size();
        return  size;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = (LayoutInflater)container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_imageve, container, false);
        PhotoView photoView = view.findViewById(R.id.bigPicView);
        String url = UrlTool.getDataLastUrl(mItems.get(position) ) ;
        Glide.with(mCtx).load(url) .into(photoView);
        // 记得加入父容器 否则是不会显示的
        container.addView(photoView);
        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)   {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object)  {
        return view == object;
    }

}